﻿$(document).ready(function () {
    $('#txtSearch').keyup(function (e) {
        var unicode = e.charCode ? e.charCode : e.keyCode;
        if (unicode == 13) {
            document.forms[0].setAttribute("onsubmit", "return false");
            search();
        }
    })
})
function search() {
    if (document.getElementById('_currentPage')) {
        document.getElementById('_currentPage').value = 1;
    }
    if (document.getElementById('bPaging')) {
        document.getElementById('bPaging').innerHTML = '';
    }
    if (document.getElementById('txtSearch').value != "") {
        if (parseInt((document.getElementById('txtSearch').value).length) > 2) {
            senddata_post('Search', 'T=search&catid=' + document.getElementById('ddlcategory').value + "&q=" + document.getElementById('txtSearch').value, 'fill');
        } else{alert("Please enter minimum 3 charachter for search.") }
    } else {alert("Please enter some value for search.")}
}
function add_arr(arr, val) {
    var count = 0;
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].toUpperCase() == val.toUpperCase()) {
            count = parseInt(count) + 1;
        }
    }
    if (parseInt(count) == 0) {
        arr.push(val);
    }
}
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement /*, fromIndex */) {
        "use strict";
        if (this == null) {
            throw new TypeError();
        }
        var t = Object(this);
        var len = t.length >>> 0;
        if (len === 0) {
            return -1;
        }
        var n = 0;
        if (arguments.length > 1) {
            n = Number(arguments[1]);
            if (n != n) { // shortcut for verifying if it's NaN
                n = 0;
            } else if (n != 0 && n != Infinity && n != -Infinity) {
                n = (n > 0 || -1) * Math.floor(Math.abs(n));
            }
        }
        if (n >= len) {
            return -1;
        }
        var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
        for (; k < len; k++) {
            if (k in t && t[k] === searchElement) {
                return k;
            }
        }
        return -1;
    }
}
function getValue(name) {
    var strArray=new Array();
    var ele = document.getElementsByName(name);
    for (var i = 0; i < ele.length; i++) {
        if (ele[i].checked) {
           strArray.push(ele[i].value);
        }
    }
    return strArray;
}
function Search_Fill(search,chk) {
    var str = "";
    var strBooks = new Array();
    var strAuthors = new Array();
    var strEdition = new Array();
    var strBookVol = new Array();
    var strPublication = new Array();
    var _currentPage = 1;
    var maxRecord = 10;
    if (document.getElementById('_currentPage')) {
        _currentPage = document.getElementById('_currentPage').value;
    }
    var firstPage = _currentPage;
    var lastPage = maxRecord;
    var tot = 0;
    firstPage = ((parseInt(_currentPage)-1) * parseInt(maxRecord))
    lastPage = ((parseInt(_currentPage) * parseInt(maxRecord)))
    var xml = search.getElementsByTagName('search');
    if (parseInt(xml.length) > 0) {
        for (var i = 0; i < xml.length; i++) {
            var count = 0;
            if (!chk) {
                var chkBooks = getValue("chkBooks");
                var chkAuthor = getValue("chkAuthor");
                var chkEdition = getValue("chkEdition");
                var chkVol = getValue("chkVol");
                var chkPub = getValue("chkPub");
                if ((chkBooks.length==0 || chkBooks.indexOf(returnValue(xml, i, 'BOOKTITLE')) != -1) && (chkAuthor.length == 0 || chkAuthor.indexOf(returnValue(xml, i, 'AUTHORS')) != -1) && ((chkVol.length == 0  || chkVol.indexOf(returnValue(xml, i, 'BOOKVOL')) != -1)) && (chkEdition.length == 0 || chkEdition.indexOf(returnValue(xml, i, 'EDITION')) != -1) && (chkPub.length == 0 || chkPub.indexOf(returnValue(xml, i, 'PUBLICATIONNAME')) != -1)) {
                    count = 1;
                }
            }
            else {
                count = 1;
            }
            if (parseInt(count) > 0) {
                tot = parseInt(tot) + 1;
                add_arr(strBooks, returnValue(xml, i, 'BOOKTITLE'))
                add_arr(strAuthors, returnValue(xml, i, 'AUTHORS'))
                add_arr(strBookVol, returnValue(xml, i, 'BOOKVOL'))
                add_arr(strEdition, returnValue(xml, i, 'EDITION'))
                add_arr(strPublication, returnValue(xml, i, 'PUBLICATIONNAME'))
                if ((parseInt(firstPage)) < parseInt(tot) && parseInt(lastPage) >= parseInt(tot)) {
                    str += "<tr >";
                    str += "<td rowspan=\"7\" style='border-bottom:1px solid #000'>";
                    str += "<div class=\"img\" style=\"margin:5px\">";
                    str += "<img alt='' src='Images/img.jpg'></div>";
                    str += "</td>";
                    str += "<td colspan='2' class='title2' style='text-align:left'>";
                    str += returnValue(xml, i, 'BOOKTITLE');

                    str += "<span style=\"float:right\">"
                    str += "<span style=\"color: #2984B3;font-weight: bold\">Total :"+returnValue(xml, i, 'TOTAL')+" &nbsp;Available :" + returnValue(xml, i, 'AVAILABLE') + "</span>";
                    str += "</span>";
                    str += "</td>";
                    str += "</tr>";
                    str += "<tr>";
                    str += "<td class='r_i_font_l' style='text-align:left'>Author :</td>";
                    str += "<td style='text-align:left'>" + returnValue(xml, i, 'AUTHORS') + "</td>";

                    str += "</tr>";
                    str += "<tr>";
                    str += "<td class='r_i_font_l' style='text-align:left'>Volume :</td>";
                    str += "<td style='text-align:left'>" + returnValue(xml, i, 'BOOKVOL') + "</td>";

                    str += "</tr>";
                    str += "<tr>";
                    str += "<td class='r_i_font_l' style='text-align:left'>Publisher :</td>";
                    str += "<td style='text-align:left'>" + returnValue(xml, i, 'PUBLICATIONNAME') + "</td>";

                    str += "</tr>";
                    str += "<tr>";
                    str += "<td class='r_i_font_l' style='text-align:left'>Clasification :</td>";
                    str += "<td style='text-align:left'>" + returnValue(xml, i, 'C_SUBJECT') + "</td>";
                    str += "</tr>";
                    str += "<tr>";
                    str += "<td class='r_i_font_l' style='text-align:left'>Edition :</td>";
                    str += "<td style='text-align:left'>" + returnValue(xml, i, 'EDITION') + "</td>";

                    str += "</tr>";
                    str += "<tr>";
                    str += "<td class='r_i_font_l' style='border-bottom:1px solid #000;text-align:left'> Here you can find book :</td>";
                    str += "<td style='border-bottom:1px solid #000;text-align:left'>" + returnValue(xml, i, 'LOCATION') + "</td>";
                }
            }
        }
    }
    if(str=="")
    {
      str="<center class='green'>No Record Found.</center>"
  }
  paging({ "totRecord": "" + tot + "", "fnName": "go();", "maxShowRecord": "" + maxRecord + "", "noOfLink": "5", "id": ["bPaging"] });
  document.getElementById('searchlist').innerHTML = '<table cellpadding="0" cellspacing="0" width="60%" class="r_i_font" >' + str + '</table>';
  if (chk) {
      document.getElementById('books').innerHTML = "<table cellpadding='0' cellspacing='0' width='100%'>" + returnHTML(strBooks, "chkBooks") + "</table>";
      document.getElementById('author').innerHTML = "<table cellpadding='0' cellspacing='0' width='100%'>" + returnHTML(strAuthors, "chkAuthor") + "</table>";
      document.getElementById('edition').innerHTML = "<table cellpadding='0' cellspacing='0' width='100%'>" + returnHTML(strEdition, "chkEdition") + "</table>";
      document.getElementById('bookvol').innerHTML = "<table cellpadding='0' cellspacing='0' width='100%'>" + returnHTML(strBookVol, "chkVol") + "</table>";
      document.getElementById('publication').innerHTML = "<table cellpadding='0' cellspacing='0' width='100%'>" + returnHTML(strPublication, "chkPub") + "</table>";
  }
}
function returnHTML(arr,id) { 
 var str="";
 for (var i = 0; i < arr.length; i++) {
     str += "<tr><td style='text-align:left'><input type='checkbox' id='" + id + i + "' name='" + id + "' value='" + arr[i] + "' onclick='searchgo();' /><label for='" + id + i + "'>" + arr[i] + "</label></td></tr>";
    }
    return str;
}
function searchgo() {
    if (document.getElementById('_currentPage')) {
        document.getElementById('_currentPage').value = 1;
    }
    if (document.getElementById('bPaging')) {
        document.getElementById('bPaging').innerHTML = '';
    }
    go();
} 
function go() {
    $.ajax({
        type: "GET",
        url: 'xls/' + document.getElementById('id').value,
        datatype: "xml",
        success: function (xml) {
        Search_Fill(xml, false);
            //t = setTimeout("parseXML()", 5000);
        }
    })
}
function stateChanged() {
    if (xmlhttp.readyState == 4) {
        if (receiveid == "fill") {
            if (xmlhttp.responseText.indexOf("~", 0) > 0) {
                var arr = xmlhttp.responseText.split("~");
                if (parseInt(arr[0]) == 1) {
                    document.getElementById('id').value = arr[1]
                    $.ajax({
                        type: "GET",
                        url: 'xls/' + arr[1],
                        datatype: "xml",
                        success: function (xml) {
                            Search_Fill(xml,true);
                            //t = setTimeout("parseXML()", 5000);
                        }
                    })
                } else {
                clearAll();
                }
            }
        }
    } else {
       document.getElementById('searchlist').innerHTML = "<tr><td><div align='center'><img src='images/loading.gif' /></div></td></tr>";
    }
}
function clearAll() {
    document.getElementById('searchlist').innerHTML = "<center class='green'>No Record Found.</center>"
    document.getElementById('books').innerHTML = "";
    document.getElementById('author').innerHTML = "";
    document.getElementById('edition').innerHTML = "";
    document.getElementById('bookvol').innerHTML = "";
    document.getElementById('publication').innerHTML = ""; 

}
function returnValue(xml_Cash, i, name) {
    var val = "";
    var eleNode = xml_Cash[i].getElementsByTagName(name);
    if (eleNode) {
        if (eleNode[0]) {
            if (eleNode[0].childNodes[0]) {
                val = eleNode[0].childNodes[0].nodeValue;
            }
        }
    }
    return val;
}